package com.lagou.ipersistence.executor.statement;

import com.lagou.ipersistence.executor.Executor;
import com.lagou.ipersistence.executor.parameter.ParameterHandler;
import com.lagou.ipersistence.mapping.BoundSql;
import com.lagou.ipersistence.mapping.MappedStatement;
import com.lagou.ipersistence.session.ResultHandler;
import com.lagou.ipersistence.session.RowBounds;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * @author Yan Jiahong
 * Created on 2020/11/22
 */
public class RoutingStatementHandler implements StatementHandler {
    private final StatementHandler delegate;
    
    public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds,
                                   ResultHandler<?> resultHandler, BoundSql boundSql) {
        switch (ms.getStatementType()) {
            case PREPARED:
                delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
                break;
            default:
                delegate = null;
                break;
        }
    }
    
    @Override
    public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
        return delegate.prepare(connection, transactionTimeout);
    }
    
    @Override
    public void parameterize(Statement statement) throws SQLException {
        delegate.parameterize(statement);
    }
    
    @Override
    public <E> List<E> query(Statement statement, ResultHandler<?> resultHandler) throws SQLException {
        return delegate.query(statement, resultHandler);
    }
    
    @Override
    public BoundSql getBoundSql() {
        return delegate.getBoundSql();
    }
    
    @Override
    public ParameterHandler getParameterHandler() {
        return delegate.getParameterHandler();
    }
    
    @Override
    public int update(Statement stmt) throws SQLException{
        return delegate.update(stmt);
    }
}
